# Hey Emacs, this is a -*- makefile -*-
#
# Copyright (C) 2013 The Paparazzi Team
#
# This file is part of paparazzi.
#
# paparazzi is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# paparazzi is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with paparazzi; see the file COPYING.  If not, see
# <http://www.gnu.org/licenses/>.
#

#
# This Makefile uses the generic Makefile.omap and adds upload rules for the ARDrone2
#

include $(PAPARAZZI_SRC)/conf/Makefile.omap

DRONE = $(PAPARAZZI_SRC)/sw/ext/ardrone2_drivers/ardrone2.py

# Allow modules or other raw makefiles to add actions to the upload
upload_extra:


# Program the device and start it.
load upload program: upload_extra $(OBJDIR)/$(TARGET).elf
	$(Q)$(DRONE) --host=$(HOST) insmod $(PAPARAZZI_SRC)/sw/ext/ardrone2_drivers/cdc-acm.ko
	$(Q)$(DRONE) --host=$(HOST) upload_file_and_run $(OBJDIR)/$(TARGET).elf $(SUB_DIR)
	$(Q)$(DRONE) --host=$(HOST) status


# Program the device and start it.
load2 upload2 program2: $(OBJDIR)/$(TARGET).elf

	# Kill the application
	-echo "killall -9 $(TARGET).elf" | telnet $(HOST)

	# Make the target dir and edit the config
	-{ 												\
	  echo "mkdir -p $(TARGET_DIR)";				\
	  echo "if grep -q \"start_paparazzi *= \" /data/config.ini; then sed -i 's/\(start_paparazzi *= *\).*/\\\1$(ARDRONE2_START_PAPARAZZI)/g' /data/config.ini; else echo \"start_paparazzi = $(ARDRONE2_START_PAPARAZZI)\" >> /data/config.ini; fi"; \
      echo "if grep -q \"wifi_mode *= \" /data/config.ini; then sed -i 's/\(wifi_mode *= *\).*/\\\1$(ARDRONE2_WIFI_MODE)/g' /data/config.ini; else echo \"wifi_mode = $(ARDRONE2_WIFI_MODE)\" >> /data/config.ini; fi"; \
      echo "if grep -q \"ssid_single_player *= \" /data/config.ini; then sed -i 's/\(ssid_single_player *= *\).*/\\\1$(ARDRONE2_SSID)/g' /data/config.ini; else echo \"ssid_single_player = $(ARDRONE2_SSID)\" >> /data/config.ini; fi"; \
      echo "if grep -q \"static_ip_address_base *= \" /data/config.ini; then sed -i 's/\(static_ip_address_base *= *\).*/\\\1$(ARDRONE2_IP_ADDRESS_BASE)/g' /data/config.ini; else echo \"static_ip_address_base = $(ARDRONE2_IP_ADDRESS_BASE)\" >> /data/config.ini; fi"; \
      echo "if grep -q \"static_ip_address_probe *= \" /data/config.ini; then sed -i 's/\(static_ip_address_probe *= *\).*/\\\1$(ARDRONE2_IP_ADDRESS_PROBE)/g' /data/config.ini; else echo \"static_ip_address_probe = $(ARDRONE2_IP_ADDRESS_PROBE)\" >> /data/config.ini; fi"; \
	} | telnet $(HOST)

	# Upload the drivers and new application
	{																								\
	  echo "binary";																				\
	  echo "put $(PAPARAZZI_SRC)/sw/ext/ardrone2_drivers/cdc-acm.ko /$(SUB_DIR)/cdc-acm.ko";		\
	  echo "put $(PAPARAZZI_SRC)/sw/ext/ardrone2_drivers/check_update.sh check_update.sh";		    \
	  echo "put $(PAPARAZZI_SRC)/sw/ext/ardrone2_drivers/wifi_setup.sh wifi_setup.sh";		        \
	  echo "put $(OBJDIR)/$(TARGET).elf /$(SUB_DIR)/$(TARGET).elf";									\
	  echo "quit";																					\
	} | ftp -n $(HOST)

	# Upload the modules and start the application
	-{ 															\
	  echo "mv /data/video/check_update.sh /bin/";				\
	  echo "mv /data/video/wifi_setup.sh /bin/";				\
	  echo "chmod 777 /bin/check_update.sh"         			\
	  echo "chmod 777 /bin/wifi_setup.sh"          			 	\
	  echo "insmod $(TARGET_DIR)/cdc-acm.ko";					\
	  echo "chmod 777 $(TARGET_DIR)/$(TARGET).elf";				\
	  echo "$(TARGET_DIR)/$(TARGET).elf > /dev/null 2>&1 &";	\
	} | telnet $(HOST)

ifeq ($(ARDRONE2_REBOOT),1)
	-{ 										\
	  echo "reboot";						\
	} | telnet $(HOST)
endif

# Listing of phony targets.
.PHONY : upload_extra load upload program load2 upload2 program2
